package me.chenqiang.tre.expression;

import java.util.function.BiFunction;

import me.chenqiang.tre.entity.Card;
import me.chenqiang.tre.entity.Device;

/**
 * 用来表示整数值关系的的表达式
 * 
 * 表达式左侧和右侧的属性都是整数值，用于判断各种数值关系。
 * 
 */
public class IntegerExpression implements Expression{
	public enum Type {
		EQ, NEQ, GT, LT, GTE, LTE
	}
	protected Type type;
	protected String propertyName;
	protected BiFunction<Card, Device, Integer> propertyGetter;
	protected int propertyValue;
	
	public IntegerExpression(Type type, String propertyName, BiFunction<Card, Device, Integer> propertyGetter,
			int value) {
		super();
		this.type = type;
		this.propertyName = propertyName;
		this.propertyGetter = propertyGetter;
		this.propertyValue = value;
	}

	@Override
	public boolean evaluate(Card card, Device device) {
		Integer value = this.propertyGetter.apply(card, device);
		switch(this.type) {
		case EQ:
			return value == this.propertyValue;
		case NEQ:
			return value != this.propertyValue;
		case GT:
			return value > this.propertyValue;
		case LT:
			return value < this.propertyValue;
		case GTE:
			return value >= this.propertyValue;
		case LTE:
			return value <= this.propertyValue;
		default:
			throw new IllegalArgumentException(String.format("错误类型：%s", this.type));
		}
	}
	

	@Override
	public String getType() {
		return this.type.name();
	}

	@Override
	public String getPropertyName() {
		return this.propertyName;
	}

	@Override
	public String getPropertyValue() {
		return Integer.toString(this.propertyValue);
	}
}
